package com.ptone.messagecenter.common.base;

public class ErrCode {
    public static final ErrCode OK = new ErrCode(0, "SUCCESS");
    public static final ErrCode FAIL = new ErrCode(500, "FAIL");
    public static final ErrCode SYSTEM_ERROR = new ErrCode(500, "系统错误");
    public static final ErrCode INTERFACE_DOES_NOT_EXIST = new ErrCode(404, "接口不存在: ");
    public static final ErrCode THE_SYSTEM_IS_BUSY = new ErrCode(10000, "系统繁忙");
    public static final ErrCode NO_ACCESS = new ErrCode(10001, "当前用户没有访问权限");
    public static final ErrCode NO_PERMISSION = new ErrCode(10003, "访问服务被拒绝");
    public static final ErrCode CHECK_PARAMS_FAIL = new ErrCode(10004, "参数校验失败");
    public static final ErrCode NO_DATA_FOR_QUERY = new ErrCode(10005, "查询无数据");
    public static final ErrCode BROWSER_CONNECTION_CLOSED = new ErrCode(10006, "浏览器关闭了连接");
    public static final ErrCode PARAMS_TYPE_MUST_BE_JSON = new ErrCode(10007, "传参类型必须为Json格式数据");
    public static final ErrCode DOWNLOAD_FILE_NOT_EXIST = new ErrCode(10008, "指定文件不存在或已删除");
    public static final ErrCode FILE_DOWNLOAD_FAIL = new ErrCode(10009, "文件下载失败");
    public static final ErrCode PLEASE_SELECT_DOWNLOAD_FILE = new ErrCode(10010, "请选择要下载的文件");
    public static final ErrCode DOWNLOAD_PATH_NOT_EXIST = new ErrCode(10011, "下载目录不存在");
    public static final ErrCode ACCOUNT_PASSWORD_DOES_NOT_MATCH = new ErrCode(100012, "用户名或密码错误");
    public static final ErrCode LOGIN_FAIL = new ErrCode(10013, "登录失败");
    public static final ErrCode ACCOUNT_NOT_EXIST = new ErrCode(10014, "用户名不存在");
    public static final ErrCode EXIT_FAIL = new ErrCode(10015, "登出失败");
    public static final ErrCode LOGIN_INFO_IS_INVALIDATION = new ErrCode(10016, "登录信息已失效，请重新登录");
    public static final ErrCode REQUEST_TYPE_NO_SUPPORT = new ErrCode(10017, "不支持的请求方式 ");
    public static final ErrCode EXCEL_CREATE_FAIL = new ErrCode(10018, "excel表格创建失败 ");
    public static final ErrCode EXCEL_EXPORT_FAIL = new ErrCode(10019, "excel表格导出失败 ");
    public static final ErrCode APP_KEY_ERROR = new ErrCode(10020, "APP_KEY错误");
    public static final ErrCode YI_TU_ERROR = new ErrCode(10021, "依图接口异常");
    public static final ErrCode MY_ERROR = new ErrCode(10022, "自定义异常");
    public static final ErrCode TOKEN_EXPIRED = new ErrCode(10023, "登录过期，请重新登录");
    public static final ErrCode TOKEN_INVALID = new ErrCode(10024, "token无效");
    public static final ErrCode JSON_ERROR = new ErrCode(10025, "json格式错误");
    public static final ErrCode BAD_SQL_GRAMMAR = new ErrCode(10026, "sql语句错误");

    public final Integer code;
    public String message;
    public Integer status;

    public static ErrCode errCode(String message) {
        return new ErrCode(message);
    }

    public ErrCode(String message) {
        this.code = 500;
        this.status = 500;
        this.message = message;
    }

    public ErrCode(Integer code, String message) {
        this.code = code;
        this.message = message;
        if (0 == code.intValue())
            this.status = 200;
    }

    public static ErrCode after(ErrCode errCode, String after) {
        return new ErrCode(errCode.code, errCode.message + after);
    }

    public ErrCode to(String message) {
        return new ErrCode(this.code, message);
    }

    @Override
    public String toString() {
        return String.format("{\"code\":%s,\"message\":\"%s\"}", code, message);
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) return false;
        if (o instanceof Integer) {
            return code.equals(o);
        } else if (o instanceof ErrCode) {
            ErrCode co = (ErrCode) o;
            return code.equals(co.code);
        } else {
            return super.equals(o);
        }
    }
}

